package com.sky.controller.user;

import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.UserAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿相关接口")
@Slf4j
@RequiredArgsConstructor
public class UserAddressBookController {
    private final UserAddressBookService userAddressBookService;

    @PostMapping
    @ApiOperation("新增地址")
    public Result<String> addAddressBook(@Validated @RequestBody AddressBook addressBook){
        userAddressBookService.addAddressBook(addressBook);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询登录用户所有地址")
    public Result<List<AddressBook>> findList(){
        List<AddressBook> list = userAddressBookService.findList();
        return Result.success(list);
    }

    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> findDefault(){
        AddressBook res = userAddressBookService.findDefault();
        return Result.success(res);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> findById(@PathVariable Long id){
        AddressBook addressBook = userAddressBookService.findById(id);
        return Result.success(addressBook);
    }

    @PutMapping
    @ApiOperation("修改地址")
    public Result<String> updateAddressBook(@Validated @RequestBody AddressBook addressBook){
        userAddressBookService.updateAddressBook(addressBook);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result<String> delById(Long id){
        userAddressBookService.delById(id);
        return Result.success();
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result<String> setDefault(@RequestBody AddressBook addressBook){
        userAddressBookService.setDefault(addressBook);
        return Result.success();
    }
}
